using UnityEngine;
using System.Collections;

public class Enemy 
{
	#region Atributos
	
	private int _level;
	
	private int _maxLife;
	private int _curLife;	
	
	private float _speed;
	
	private int _damageMin;
	private int _damageMax;
	
	private Sprite2D _sprEnemy;
	
	private const int X_POS_TO_ATTACK = 100;
	
	private float _tempo = 0;
	private const int ATTACK_DELAY_IN_SECONDS = 2;
	
	private Sprite2D _sprBackHealth;
	private Sprite2D _sprHealth;
	
	private bool atacou = false;
	
	private Sprite2D _sprBackLevel;
	
	private GUIStyle gStyle;

    #endregion
	
	#region Construtor
	
	public Enemy(int level) 
	{
		Level = level;
		
		_sprEnemy = new Sprite2D("Images/Game Elements/Enemy", new Vector2(Random.Range(Constantes.TELA_LARGURA, Constantes.TELA_LARGURA * 1.5f),
												Random.Range(0, Constantes.TELA_ALTURA - 100)));
		
		_sprBackHealth = new Sprite2D("Images/GUI/BackLifeBar", new Vector2(_sprEnemy.Position.x, _sprEnemy.Position.y - 25));
		_sprHealth = new Sprite2D("Images/GUI/LifeBar", new Vector2(_sprEnemy.Position.x, _sprEnemy.Position.y - 25));
		
		_sprBackLevel = new Sprite2D("Images/GUI/LevelBack", new Vector2(_sprEnemy.Position.x - 50, _sprBackHealth.Position.y - 15));
		
		gStyle = new GUIStyle();
		gStyle.fontSize = 25;
		gStyle.normal.textColor = Color.white;
	}
	
	#endregion
	
	#region Propriedades
	
	public int Level
	{
		get { return _level; }
		set 
		{ 
			_level = value; 
			
			DefineAtributos();
		}
	}
	
	public int Damage
	{
		get { return Random.Range(_damageMin, _damageMax); }
	}
	
	public int MaxLife
	{
		get { return _maxLife; }
		set { _maxLife = value; }
	}
	
	public int CurrentLife
	{
		get { return _curLife; }
		set 
		{ 
			_curLife = value; 
			
			if(_curLife < 0)
				_curLife = 0;
		}
	}

    public Rect CollisionRect { get; private set; }
	
	public bool AtacouPlayer
	{
		get { return atacou; }
		set { atacou = value; }
	}
	
	#endregion
	
	#region Update
	
	public void Update() 
	{
		Movimentar();
		Atacar();
	}
	
	#endregion
	
	#region Draw
	
	public void Draw()
	{
		_sprEnemy.Draw();
		
		_sprBackHealth.Draw();
		
		float lifePercent = (float)_curLife / (float)_maxLife;
	
		_sprHealth.Draw(new Rect(0, 0, lifePercent, 1));
		_sprBackLevel.Draw();
		
		
		GUI.Label(new Rect(_sprBackLevel.Position.x + 13, _sprBackLevel.Position.y + 7, 50, 50), _level.ToString(), gStyle);
	}
	
	#endregion
	
	#region Métodos
	
	private void DefineAtributos()
	{
		_maxLife = _level * 100;
		_curLife = _maxLife;
		
		_speed = 3;
	
		_damageMin = 1;
		_damageMax = 5;
	}
	
	private void Movimentar()
	{
		if(_sprEnemy.Position.x > X_POS_TO_ATTACK)
			_sprEnemy.Position = new Vector2(_sprEnemy.Position.x - _speed, _sprEnemy.Position.y);
		
		else if(_sprEnemy.Position.x < X_POS_TO_ATTACK)
			_sprEnemy.Position = new Vector2(X_POS_TO_ATTACK, _sprEnemy.Position.y);
		
        CollisionRect = new Rect(_sprEnemy.Position.x, _sprEnemy.Position.y, _sprEnemy.Largura, _sprEnemy.Altura);

		_sprBackHealth.Position = new Vector2(_sprEnemy.Position.x, _sprEnemy.Position.y - 25);
		_sprHealth.Position = new Vector2(_sprEnemy.Position.x, _sprEnemy.Position.y - 25);
		_sprBackLevel.Position = new Vector2(_sprEnemy.Position.x - 50, _sprBackHealth.Position.y - 15);
	}
	
	private void Atacar()
	{
		if(_sprEnemy.Position.x != X_POS_TO_ATTACK) return;
		
		_tempo += Time.deltaTime;
		
		if(_tempo >= ATTACK_DELAY_IN_SECONDS)
		{
			_tempo = 0;
			atacou = true;
		}
	}
	
	#endregion
}
